package com.efounder.adapter;


import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.efounder.activity.ProductDetailsActivity;
import com.efounder.bean.CollectList;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.mobilemall.R;
import com.efounder.utils.MallRequestUtil;
import com.efounder.utils.MyMathUtil;
import com.utilcode.util.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static com.efounder.utils.MallRequestUtil.GOOD_ADDTOCART_URL;

/**
 * Created by wei on 2018/2/28/028.
 */

public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<CollectList.CollectGoodsBean> collectDatas;
    private String TAG = this.getClass().getSimpleName();


    public CollectAdapter(Context context, ArrayList<CollectList.CollectGoodsBean> datats) {
        mContext = context;
        collectDatas = datats;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collect, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(mContext).load(collectDatas.get(position).getGoodCover()).into(holder.goodImage);
        holder.goodTitle.setText(collectDatas.get(position).getGoodName());
        double price = collectDatas.get(position).getMallGoodPrice().getPrice();
        holder.goodPrice.setText(collectDatas.get(position).getMallGoodPrice().getPriceUnit()  + " " + MyMathUtil.round3(price,2));
        holder.godetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProductDetailsActivity.class);
                intent.putExtra("goodbh", collectDatas.get(position).getGoodId());
                mContext.startActivity(intent);
            }
        });

        holder.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 18-10-22  增加SKUID
                addToCart(collectDatas.get(position).getGoodId(), "1");
            }
        });

    }

    @Override
    public int getItemCount() {
        return collectDatas.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView goodImage;
        TextView goodTitle;
        TextView goodPrice;
        TextView buy;
        RelativeLayout godetail;
        Button btnDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            goodImage = itemView.findViewById(R.id.image);
            goodTitle = itemView.findViewById(R.id.title);
            goodPrice = itemView.findViewById(R.id.price);
            buy = itemView.findViewById(R.id.buy);
            godetail = itemView.findViewById(R.id.godetail);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onDeleteClickListener.onDelete(getAdapterPosition());
                }
            });

        }
    }

    public void addToCart(String goodbh, String skuId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());
        map.put("goodbh", goodbh);
        map.put("skuId", skuId);
        map.put("goodNum", "1");
        MallRequestUtil.getRequest(TAG, GOOD_ADDTOCART_URL, map, new MallRequestUtil.MallRequestCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    if ("success".equals(object.getString("result"))) {
                        Toast.makeText(mContext, R.string.mail_join_car_success, Toast.LENGTH_SHORT).show();
                    } else if ("fail".equals(object.optString("result"))){
                        ToastUtils.showShort(object.optString("msg"));
                    } else {
                        ToastUtils.showShort(R.string.mail_join_car_fail);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(String error) {
                Toast.makeText(mContext, R.string.mail_join_car_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }
    private OnDeleteClickListener onDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public interface OnDeleteClickListener{
        void onDelete(int position);
    }
}
